<template>
	<view>
		<page-head :title="'地址列表'" :headtype="2"></page-head>
		<view class="siteitem marginAuto" v-for="(item,index) in datas" :key="index">
			<view class="siteitem-top blockbg marginAuto">
				<view class="siteitemtl" @click="defafun(item,index)"
					:class="item.is_default == 1 ? 'activepaytypeir':''">
					<u-icon class="marginAuto" name="checkbox-mark" color="#E5E5E5" size="30"></u-icon>
				</view>
				<view class="siteitemtr" @click="defafun(item,index)">
					<view class="namephone">
						<text style="font-size:32rpx;">{{ item.username }}</text>
						<text style="font-size:28rpx;margin-left:8rpx;">{{ item.mobile }}</text>
						<view class="defatxt mainBg" v-if="item.is_default == 1">默认</view>
					</view>
					<view class="sitedec ellipsis02">{{ item.full_address }}</view>
				</view>
				<view class="sitebtnicon">
					<!-- <u-icon @click="delfun(item,index)" class="siteitemb-del" name="trash" color="#000" size="50"></u-icon> -->
					<image @click="apifun.navigate('/pages/my/siteadd','id='+item.id);" class="siteitemb-com"
						style="width:30rpx;height:30rpx;" src="@/static/my/bj.png" mode="aspectFill"></image>
				</view>
			</view>
			<!-- <view class="siteitem-bottom blockbg">
				<view class="siteitemb-defa" @click="defafun(item,index)">
					<view class="siteitembicon" :class="siteac == index ? 'activepaytypeir':''">
						<u-icon class="marginAuto" name="checkmark" :color="siteac == index ? '#E5E5E5':'#fff'"
							size="26"></u-icon>
					</view>
					<view class="siteitembtxt">设置为默认地址</view>
				</view>
			</view> -->
		</view>
		<view class="addsite-btn">
			<u-button @click="apifun.navigate('/pages/my/siteadd')" class="addsite-btn" color="#9E190F" text="添加收货地址"
				:customStyle="{
				border:'none',
				borderRadius:'10rpx',
				height:'88rpx',
				lineHeight:'88rpx'
				}"></u-button>
		</view>
		<dataempty v-if="datas.length <= 0"></dataempty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				datas:[],
				apifun:this.apifun
			}
		},
		onShow() {
			this.initData()
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type
			}
		},
		methods: {
			defafun(item, index) {
				if (this.type == "order") {  // pages/order/order_detail
					let pages = getCurrentPages();
					let prevPage = pages[pages.length - 2];
					prevPage.$vm.setAddr(item);
					uni.navigateBack({
						delta: 1
					})
				}else if (this.type == "pay"){  // pages/homeList/payment/payment  | pages/homeList/integral/integral |  pages/homeList/calculate/calculate
					let pages = getCurrentPages();
					let prevPage = pages[pages.length - 2];
					prevPage.$vm.siteac = item;
					uni.navigateBack({
						delta: 1
					})
				}else {
					// 默认
					this.siteac = index;
					let sendData = {
						id: item.id,
						is_default: 1
					};
					this.apifun.unirequest('/api/address/setUserAddress', 'post', sendData, (res) => {
						// console.log(res)
						if (res.code === 200) {
							let datas = res.data;
							this.datas.forEach((item) => {
								this.$set(item, 'is_default', 0)
							});
							this.datas[index].is_default = 1;
						} else {
							this.apifun.toast(res.msg)
						}
					})
				}
			},
			// delfun(item,index){  // 删除
			// 	this.datas.splice(index,1)
			// },
			initData() {
				let sendData = {};
				this.apifun.unirequest('/api/address/getUserAddress', 'post', sendData, (res) => {
					// console.log(res)
					if (res.code === 200) {
						let datas = res.data;
						this.datas = datas;
					} else {
						this.apifun.toast(res.msg)
					}
				})
			}
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh();
		},
	}
</script>

<style scoped lang="scss">
	page {
		padding-bottom: 150rpx;
	}

	.blockbg {
		width: 640rpx;
		padding: 24rpx;
		// border-radius: 20rpx;
		// background-color: #fff;
		// margin-top: 20rpx;
	}

	.siteitem {
		width: 90%;
		border-radius: 20rpx;
		background-color: #fff;
		margin-top: 20rpx;
	}

	.siteitem-top {
		position: relative;
		border-bottom: 1px solid #F7F7F7;
		display: flex;

		.siteitemtl {
			width: 30rpx;
			height: 30rpx;
			border-radius: 50%;
			display: flex;
			background-color: #E5E5E5;
		}

		.siteitemtr {
			width: 80%;
			margin-left: 20rpx;

			.namephone {
				display: flex;
				color: #333333;
				line-height: 40rpx;

				.defatxt {
					padding: 0 14rpx;
					height: 40rpx;
					line-height: 40rpx;
					border-radius: 10rpx;
					color: #fff;
					font-size: 26rpx;
					margin-left: 20rpx;
				}
			}

			.sitedec {
				font-size: 24rpx;
				color: #666666;
				line-height: 40rpx;
				height: 80rpx;
				margin-top: 22rpx;
			}
		}

		.sitebtnicon {
			position: absolute;
			right: 24rpx;
			width: 50rpx;
			text-align: center;

			.siteitemb-com {
				margin-top: 60rpx;
			}
		}
	}

	.siteitem-bottom {
		position: relative;
		display: flex;
		justify-content: space-between;

		.siteitemb-defa {
			display: flex;
			margin-top: 5rpx;

			.siteitembicon {
				width: 36rpx;
				height: 36rpx;
				border-radius: 50%;
				background-color: #fff;
				border: 1px solid #999999;
				display: flex;
			}

			.siteitembtxt {
				margin-left: 12rpx;
				color: #666666;
				font-size: 24rpx;
				line-height: 40rpx;
			}
		}
	}

	.addsite-btn {
		position: fixed;
		bottom: 50rpx;
		width: 90%;
		height: 88rpx;
		line-height: 88rpx;
		padding: 6rpx 5% 20rpx;
		background-color: #fff;
	}

	.activepaytypeir {
		border-color: #9E190F !important;
		background-color: #9E190F !important;
	}
</style>